#include "PushLevelExitTrigger.h"
#include "MyGame.h"
#include "Level.h"
#include "Factory.h"

PushLevelExitTrigger::PushLevelExitTrigger(const Vector2df p, const Vector2df s) : LevelExitTrigger(p,s) {
    m_counter.init(100,MILLI_SECONDS,this,PushLevelExitTrigger::countDownWrapper);
    m_counter.setRewind(REWIND);
    m_active = true;
}

PushLevelExitTrigger::PushLevelExitTrigger(const Vector2df p, const Vector2df s, string p_levelFileName) : LevelExitTrigger(p,s) {
	levelFileName = p_levelFileName;
    m_counter.init(1000,MILLI_SECONDS,this,PushLevelExitTrigger::countDownWrapper);
    m_counter.setRewind(REWIND);
    m_active = true;
}

PushLevelExitTrigger::~PushLevelExitTrigger() {

}

//----------------------
// public declarations
//----------------------

void PushLevelExitTrigger::update()
{
    BaseTrigger::update();
    m_counter.update();
}

void PushLevelExitTrigger::hit(const SimpleDirection dir, BaseUnit* player) {
    if (m_active) {
        // The player hit the exit, let's load the next level
        MyGame *game = MyGame::GetMyGame();
        Level *level = game->GetCurrentLevel();
        if (!level) {
            // Somehow we aren't in a level; complain and die
            perror("Critical error: Failed to get current level.");
            exit(-1);
        }

        WandorBaseState *newLevel;
        // nevermind, I figured it out
        Factory *f = Factory::GetFactory();
        newLevel = f->GetLevel(levelFileName);

        newLevel->Activate();
        game->PushState(newLevel);
        m_active = false;
        m_counter.start();
    }
}

void PushLevelExitTrigger::customUpdate()
{
    //m_active = true;
    // can't activate the trigger here as it will be triggered right again when the player returns to the pushed level
}
